<template>
  <el-dialog
    :title="!dataForm.seckilId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品价格" prop="goodsPrice">
      <el-input v-model="dataForm.goodsPrice" placeholder="商品价格"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="goodsName">
      <el-input v-model="dataForm.goodsName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品库存" prop="goodsKucun">
      <el-input v-model="dataForm.goodsKucun" placeholder="商品库存"></el-input>
    </el-form-item>
    <el-form-item label="商品图片" prop="goodsImg">
      <el-input v-model="dataForm.goodsImg" placeholder="商品图片"></el-input>
    </el-form-item>
    <el-form-item label="秒杀价格" prop="seckilPrice">
      <el-input v-model="dataForm.seckilPrice" placeholder="秒杀价格"></el-input>
    </el-form-item>
    <el-form-item label="秒杀库存" prop="seckilKucun">
      <el-input v-model="dataForm.seckilKucun" placeholder="秒杀库存"></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="seckilStartAt">
      <el-input v-model="dataForm.seckilStartAt" placeholder="开始时间"></el-input>
    </el-form-item>
    <el-form-item label="结束时间" prop="seckilEndAt">
      <el-input v-model="dataForm.seckilEndAt" placeholder="结束时间"></el-input>
    </el-form-item>
    <el-form-item label="发起时间" prop="seckilCreateAt">
      <el-input v-model="dataForm.seckilCreateAt" placeholder="发起时间"></el-input>
    </el-form-item>
    <el-form-item label="限购设置" prop="seckilCount">
      <el-input v-model="dataForm.seckilCount" placeholder="限购设置"></el-input>
    </el-form-item>
    <el-form-item label="订单取消" prop="seckilCancel">
      <el-input v-model="dataForm.seckilCancel" placeholder="订单取消"></el-input>
    </el-form-item>
    <el-form-item label="活动提醒" prop="seckilRemind">
      <el-input v-model="dataForm.seckilRemind" placeholder="活动提醒"></el-input>
    </el-form-item>
    <el-form-item label="分享描述" prop="seckilDescribe">
      <el-input v-model="dataForm.seckilDescribe" placeholder="分享描述"></el-input>
    </el-form-item>
    <el-form-item label="秒杀状态" prop="seckilStatus">
      <el-input v-model="dataForm.seckilStatus" placeholder="秒杀状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          seckilId: 0,
          goodsPrice: '',
          goodsName: '',
          goodsKucun: '',
          goodsImg: '',
          seckilPrice: '',
          seckilKucun: '',
          seckilStartAt: '',
          seckilEndAt: '',
          seckilCreateAt: '',
          seckilCount: '',
          seckilCancel: '',
          seckilRemind: '',
          seckilDescribe: '',
          seckilStatus: ''
        },
        dataRule: {
          goodsPrice: [
            { required: true, message: '商品价格不能为空', trigger: 'blur' }
          ],
          goodsName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          goodsKucun: [
            { required: true, message: '商品库存不能为空', trigger: 'blur' }
          ],
          goodsImg: [
            { required: true, message: '商品图片不能为空', trigger: 'blur' }
          ],
          seckilPrice: [
            { required: true, message: '秒杀价格不能为空', trigger: 'blur' }
          ],
          seckilKucun: [
            { required: true, message: '秒杀库存不能为空', trigger: 'blur' }
          ],
          seckilStartAt: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          seckilEndAt: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ],
          seckilCreateAt: [
            { required: true, message: '发起时间不能为空', trigger: 'blur' }
          ],
          seckilCount: [
            { required: true, message: '限购设置不能为空', trigger: 'blur' }
          ],
          seckilCancel: [
            { required: true, message: '订单取消不能为空', trigger: 'blur' }
          ],
          seckilRemind: [
            { required: true, message: '活动提醒不能为空', trigger: 'blur' }
          ],
          seckilDescribe: [
            { required: true, message: '分享描述不能为空', trigger: 'blur' }
          ],
          seckilStatus: [
            { required: true, message: '秒杀状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.seckilId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.seckilId) {
            this.$http({
              url: this.$http.adornUrl(`/seckil/seckill/info/${this.dataForm.seckilId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.goodsPrice = data.seckill.goodsPrice
                this.dataForm.goodsName = data.seckill.goodsName
                this.dataForm.goodsKucun = data.seckill.goodsKucun
                this.dataForm.goodsImg = data.seckill.goodsImg
                this.dataForm.seckilPrice = data.seckill.seckilPrice
                this.dataForm.seckilKucun = data.seckill.seckilKucun
                this.dataForm.seckilStartAt = data.seckill.seckilStartAt
                this.dataForm.seckilEndAt = data.seckill.seckilEndAt
                this.dataForm.seckilCreateAt = data.seckill.seckilCreateAt
                this.dataForm.seckilCount = data.seckill.seckilCount
                this.dataForm.seckilCancel = data.seckill.seckilCancel
                this.dataForm.seckilRemind = data.seckill.seckilRemind
                this.dataForm.seckilDescribe = data.seckill.seckilDescribe
                this.dataForm.seckilStatus = data.seckill.seckilStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/seckil/seckill/${!this.dataForm.seckilId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'seckilId': this.dataForm.seckilId || undefined,
                'goodsPrice': this.dataForm.goodsPrice,
                'goodsName': this.dataForm.goodsName,
                'goodsKucun': this.dataForm.goodsKucun,
                'goodsImg': this.dataForm.goodsImg,
                'seckilPrice': this.dataForm.seckilPrice,
                'seckilKucun': this.dataForm.seckilKucun,
                'seckilStartAt': this.dataForm.seckilStartAt,
                'seckilEndAt': this.dataForm.seckilEndAt,
                'seckilCreateAt': this.dataForm.seckilCreateAt,
                'seckilCount': this.dataForm.seckilCount,
                'seckilCancel': this.dataForm.seckilCancel,
                'seckilRemind': this.dataForm.seckilRemind,
                'seckilDescribe': this.dataForm.seckilDescribe,
                'seckilStatus': this.dataForm.seckilStatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
